{% requireAdmin %}

{% extends "settings/users/_layout" %}
{% set selectedNavItem = 'settings' %}

{% import "_includes/forms" as forms %}


{% if settings is not defined %}
    {% set settings = craft.app.projectConfig.get('users') ?? [] %}
{% endif %}

{# set defaults #}
{% set settings = {
    photoVolumeUid: null,
    photoSubpath: null,
    requireEmailVerification: true,
    allowPublicRegistration: false,
    suspendByDefault: false,
    defaultGroup: null,
}|merge(settings) %}

{% set allVolumes = craft.app.volumes.getAllVolumes() %}
{% set volumeList = [] %}
{% for volume in allVolumes %}
    {% set volumeList = volumeList|merge([{'value': volume.uid, 'label': volume.name}]) %}
{% endfor %}

{% macro assetLocationInput(volumeOptions, volumeUid, subpath) %}
    {% from "_includes/forms" import select, text %}
    <div class="flex">
        <div>
            {{ select({
                id: 'photoVolumeUid',
                name: 'photoVolumeUid',
                options: volumeOptions,
                value: volumeUid,
            }) }}
        </div>
        <div class="flex-grow">
            {{ text({
                id: 'photoSubpath',
                class: 'ltr',
                name: 'photoSubpath',
                value: subpath,
                placeholder: "path/to/subfolder"|t('app')
            }) }}
        </div>
    </div>
{% endmacro %}

{% from _self import assetLocationInput %}

{% block content %}
    <form id="settings-form" method="post" class="centered" accept-charset="UTF-8" data-saveshortcut>
        {{ actionInput('user-settings/save-user-settings') }}
        {{ csrfInput() }}

        {% if volumeList %}
            {{ forms.field({
                first: true,
                label: "User Photo Location"|t('app'),
                instructions: "Where do you want to store user photos? Note that the subfolder path can contain variables like <code>{username}</code>."|t('app')
            }, assetLocationInput(volumeList, settings.photoVolumeUid, settings.photoSubpath)) }}
        {% else %}
            {{ forms.field({
                first: true,
                label: "User Photo Volume"|t('app')
            }, '<p class="error">' ~ "No volumes exist yet."|t('app') ~ '</p>') }}
        {% endif %}

        {% if CraftEdition == CraftPro %}
            {{ forms.checkboxField({
                label: 'Verify email addresses'|t('app'),
                instructions: "Should new email addresses be verified before getting saved to user accounts? (This also affects new user registration.)"|t('app'),
                name: 'requireEmailVerification',
                checked: settings.requireEmailVerification,
            }) }}

            {{ forms.checkboxField({
                label: 'Allow public registration'|t('app'),
                name: 'allowPublicRegistration',
                checked: settings.allowPublicRegistration,
                toggle: 'publicRegistrationSettings'
            }) }}

            <div id="publicRegistrationSettings" class="nested-fields{% if not settings.allowPublicRegistration %} hidden{% endif %}">
                {{ forms.checkboxField({
                    label: 'Suspend users by default'|t('app'),
                    instructions: 'Should users who register their own accounts be suspended by default? This will prevent them from receiving an activation email.'|t('app'),
                    name: 'suspendByDefault',
                    checked: settings.suspendByDefault,
                }) }}

                {% set groups = [{ label: "None"|t('app'), value: '' }] %}
                {% for group in craft.app.userGroups.getAllGroups() %}
                    {% set groups = groups|merge([{ label: group.name, value: group.uid }]) %}
                {% endfor %}

                {{ forms.selectField({
                    label: "Default User Group"|t('app'),
                    instructions: "Choose a user group that publicly-registered members will be added to by default."|t('app'),
                    name: 'defaultGroup',
                    options: groups,
                    value: settings.defaultGroup
                }) }}
            </div>
        {% endif %}

        <div class="buttons">
            <button type="submit" class="btn submit">{{ 'Save'|t('app') }}</button>
        </div>
    </form>
{% endblock %}
